<?php

class SubcategoryController extends AController
{
    public $_model;

    public function actionCreate()
    {
        $model = new Category;

        $this->performAjaxValidation($model);
        if (isset($_POST['Category'])) {
            $model->attributes = $_POST['Category'];
            $model->is_submenu = 1;
            if ($model->save()) {
                $this->redirect(array('/admin/subcategory/create'));
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate()
    {
        $model = $this->loadModel();

        $this->performAjaxValidation($model);

        if (isset($_POST['Category'])) {
            $model->attributes = $_POST['Category'];
            if ($model->save()) {
                $this->redirect(array('/admin/subcategory/create'));
            }
        }

        $this->render('update', array(
            'model' => $model
        ));
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     */
    public function actionDelete($id)
    {
        $listProduct = Products::model()->findAll("category_id ='" . $id . "'");

        if($listProduct) {
            foreach($listProduct as $product) {
                $listOrder = OrderPosition::model()->findAll("product_id ='" . $product->product_id . "'");
                if($listOrder) {
                    OrderPosition::model()->deleteAll("product_id ='" . $product->product_id . "'");
                    foreach($listOrder as $order) {
                        Order::model()->deleteAll("order_id ='" . $order->order_id . "'");
                    }
                }
                Image::model()->deleteAll("product_id ='" . $product->product_id . "'");
            }
        }
        Products::model()->deleteAll("category_id ='" . $id . "'");
        $this->loadModel()->delete();

        Yii::app()->user->setFlash('message', "Xóa sub menu thành công!");

        $this->redirect(array('/admin/subcategory/admin'));
    }

    /**
     * Lists all models.
     */
    public function actionIndex()
    {
        $dataProvider = new CActiveDataProvider('Category');
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    /**
     * Manages all models.
     */
    public function actionAdmin()
    {
        $model = new Category('search');
        if (isset($_GET['Category']))
            $model->attributes = $_GET['Category'];

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     */
    public function loadModel()
    {
        if ($this->_model === null) {
            if (isset($_GET['id']))
                $this->_model = Category::model()->findbyPk($_GET['id']);
            if ($this->_model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $this->_model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model)
    {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'category-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
    
    /*
     * ajax update order category
     */
    public function actionUpdateOrder() 
    {
        if(isset($_POST['catId']))
        {
            $categoryModel = new Category();
            if($_POST['down'] == 1) 
                $result = $categoryModel->updateSubOrderDown($_POST['catId']);
            else
                $result = $categoryModel->updateSubOrderUp($_POST['catId']);
        }
    }
    
    /*
     * save order subcategory
     */
    public function actionSaveOrder() 
    {
       if(is_array($_POST)) { 
            foreach($_POST['id'] as $key=>$id) {
                Category::model()->updateByPk($id, array('order'=>$_POST['order'][$key]));
            }
        }
        
        $this->redirect('/admin/subcategory/create');
    }
}
